package com.atguigu.exam.controller;

import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Banner;
import com.atguigu.exam.properties.MinioProperties;
import com.atguigu.exam.service.BannerService;
import com.atguigu.exam.service.FileUploadService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Tag(name = "轮播图相关接口")
@RestController
//@CrossOrigin  // 允许跨域访问
@RequestMapping("/api/banners")
@EnableConfigurationProperties(MinioProperties.class)
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @Autowired
    private FileUploadService fileUploadService;

    @Operation(summary = "获取所有轮播图列表")
    @GetMapping("/list")
    public Result<List<Banner>> getBannerList() {
        //需要进行排序
        LambdaQueryWrapper<Banner> bannerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bannerLambdaQueryWrapper.orderByAsc(Banner::getSortOrder);
        //调用BannerService中的list方法
        List<Banner> list = bannerService.list(bannerLambdaQueryWrapper);
        return Result.success(list);
    }

    @Operation(summary = "获取启用的轮播图")
    @GetMapping("/active")
    public Result<List<Banner>> getActiveBanner() {
        //设置条件
        LambdaQueryWrapper<Banner> bannerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bannerLambdaQueryWrapper.eq(Banner::getIsActive, true);
        List<Banner> list = bannerService.list(bannerLambdaQueryWrapper);
        return Result.success(list);
    }

    @Operation(summary = "根据ID获取轮播图")
    @GetMapping("/{id}")
    public Result<Banner> getById(@PathVariable Long id) {
        Banner banner = bannerService.getById(id);
        return Result.success(banner);
    }

    @Operation(summary = "添加轮播图")
    @PostMapping("/add")
    public Result addBanner(@RequestBody Banner banner) {
        boolean save = bannerService.save(banner);
        if (save) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @Operation(summary = "更新轮播图")
    @PutMapping("/update")
    public Result updateBanner(@RequestBody Banner banner) {
        boolean update = bannerService.updateById(banner);
        if (update) {
            return Result.success("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    @Operation(summary = "删除轮播图")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id) {
        boolean remove = bannerService.removeById(id);
        if (remove) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    @Operation(summary = "切换轮播图状态")
    @PutMapping("/toggle/{id}")
    public Result changeToggle(@PathVariable Long id, @RequestParam Boolean isActive) {
        LambdaUpdateWrapper<Banner> bannerLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        bannerLambdaUpdateWrapper.set(Banner::getIsActive, isActive)
                .set(Banner::getUpdateTime, new Date())
                .eq(Banner::getId, id);
        boolean update = bannerService.update(bannerLambdaUpdateWrapper);
        if (update) {
            return Result.success("切换成功");
        } else {
            return Result.error("切换失败");
        }

    }

    @Operation(summary = "上传轮播图图片")
    @PostMapping("/upload-image")
    public Result<String> uploadBannerImage(@RequestParam("file") @RequestPart MultipartFile file) {
        String imgUrl = fileUploadService.fileUpload(file);
        return Result.success(imgUrl, "图片上传成功");
    }
}
